package com.smiier.skin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.UserCheckRegistTask;
import com.smiier.skin.net.UserCheckRegistTask.UserCheckRegistRequest;
import com.smiier.skin.net.UserCheckRegistTask.UserCheckRegistResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

/**
 * @{# RegisterActivity.java Create on 2014-5-26 下午9:51:05
 * 
 * @author <a href="mailto:evan0502@qq.com">Evan</a>
 * @version 1.0
 * 
 *          此界面比较重，用于注册，找回密码，初始支付密码，绑定手机
 */

public class RegisterFirstStepActivity extends BasicActivity {

	private TextView text_agree;
	private EditText edit_tel;

	private int operateType;

	private Button btn_register;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_1);
		init();

		operateType = getIntent().getIntExtra(Constant.IDENTITY_KEY_1, 0);

		btn_register = (Button) findViewById(R.id.btn_register);
		text_agree = (TextView) findViewById(R.id.text_agree);
		edit_tel = (EditText) findViewById(R.id.edit_tel);

		if (operateType == 0) {
			//2为第三方登录
			if(GlobalSettings.login_type == 2){
				
				setNavTitle("绑定您的手机号");
				setNavLeftBtn("登录");
				
			}else{
				setNavTitle("注册");
				setNavLeftBtn("登录");
			}
		} else if (operateType == Constant.TYPE_UPDATE_PASSWORD) {
			setNavTitle("修改密码");
			setNavLeftBtn("系统设置");
			btn_register.setText("提交");
			text_agree.setVisibility(View.GONE);
		} else if (operateType == Constant.TYPE_FORGET_PASSWORD) {
			setNavTitle("忘记密码");
			btn_register.setText("提交");
			text_agree.setVisibility(View.GONE);
		} else if (operateType == Constant.TYPE_RESET_PAY_PASSWORD) {
			setNavTitle("重置支付密码");
			btn_register.setText("提交");
			text_agree.setVisibility(View.GONE);
		} else if (operateType == Constant.TYPE_UPDATE_MOBILE) {
			setNavTitle("输入手机号码");
			btn_register.setText("下一步");
			text_agree.setVisibility(View.GONE);
		}

		String content = "<font color='#7a7a7a'>点击上面的“注册”按钮，即表示你接受并同意《</font><font color='#36d49a'>皮肤科医生软件使用许可及服务协议</font>》</font>";
		text_agree.setText(Html.fromHtml(content));
	}

	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_left) {
			finish();
		} else if (id == R.id.btn_register) {
			final String tel = edit_tel.getText().toString();
			if (!CommonUtility.isNull(tel)) {
				if (operateType != 0
						&& operateType != Constant.TYPE_UPDATE_MOBILE) {
					CommonUtility.confirmSendCode(this, tel, operateType);
				} else {
					UserCheckRegistRequest req = new UserCheckRegistRequest();
					req.mobile = tel;
					UserCheckRegistTask task = new UserCheckRegistTask();
					task.setRequest(req);
					task.addListener(new NetTaskListener<UserCheckRegistTask.UserCheckRegistRequest, UserCheckRegistTask.UserCheckRegistResponse>() {

						@Override
						public void onException(
								INetTask<UserCheckRegistRequest, UserCheckRegistResponse> task,
								Exception e) {
						}

						@Override
						public void onComplete(
								INetTask<UserCheckRegistRequest, UserCheckRegistResponse> task,
								UserCheckRegistResponse response) {
							if (response.ResultCode != 200) {
								return;
							}
							try {
								int res = response.Res;
								if (1 == res) {
									CommonUtility.confirmSendCode(
											(Activity) getContext(), tel,
											operateType);
								} else if (0 == res) {
									toast("手机号码已经注册");
								} else if (-1002 == res) {
									toast("手机号码不能为空");
								}

							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
					add(task);
				}
			} else {
				toast("请输入手机号");
			}
		} else if (id == R.id.text_agree) {
			startActivity(new Intent(this, ShengmingActivity.class));
		}
	}
	
	
	/*@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
